package com.sunnada.edu.business.salaryManage.service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import com.sunnada.edu.system.pub.common.AppContext;
import com.sunnada.edu.system.pub.common.MD5;
import com.sunnada.edu.system.pub.dao.PubDao;
import com.sunnada.edu.system.pub.util.Utils;

public class ExitSettlementMethods {

	public static List<HashMap<String, String>> getStatistics(List workerList) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");// 设置日期格式
		String recTime = df.format(new Date());// 当前月份

		// 获取所有当前在职工人
		List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		for (int i = 0; i < workerList.size(); i++) {
			HashMap<String, String> worker = (HashMap<String, String>) workerList.get(i);
			String workerId = worker.get("REC_ID");
			String recId = Utils.getTimeSerialNumber();
			worker.put("recId", recId);// 某工人工资18位工资流水号
			worker.put("workerId", workerId);
			worker.put("recTime", recTime);// 工资月份
			/***********************************/

			// 获取某个工人本月考勤总工时
			worker.put("workHours", getWorkHours(worker));
			// 获取某个工人本月累计工作量
			worker.put("workQuantity", getWorkQuantity(worker));
			// 获取某个工人本月奖金
			worker.put("workReward", getWorkReward(worker));
			// 获取某个工人本月补贴
			worker.put("workSubsidy", getWorkSubsidy(worker));
			// 获取某个工人本月考勤扣款
			worker.put("attDeduction", getWorkAttDeduction(worker));
			// 其他扣款
			worker.put("otherDeduction", getWorkOtherDeduction(worker));
			// 获取某个工人本月五险一金
			worker.put("wxyj", getWorkWxyj(worker));
			/***********************************/

			Map workHisSalary = getHisSalaryByWorkId(worker);
			Double hisShouldGive = (Double) workHisSalary.get("SHOULD_GIVE");
			Double hisRealGive = (Double) workHisSalary.get("REAL_GIVE");
			Double hisNoGive = (Double) workHisSalary.get("NO_GIVE");
			Double hisQuantity = (Double) workHisSalary.get("WORK_QUANTITY");
			Double hisHours = (Double) workHisSalary.get("WORK_HOURS");
			// 获取某个工人本月考勤总工时
			worker.put("WORKHOURS", coutAdd(getWorkHours(worker), hisHours));
			// 获取某个工人累计工作量
			worker.put("WORKQUANTITY", coutAdd(getWorkQuantity(worker), hisQuantity));

			// 获取某个工人本月应发
			worker.put("shouldGive", getWorkShouldGived(worker));
			// 获取某个工人本月实发
			worker.put("realGive", getWorkRealGived(worker));
			// 获取某个工人本月未发
			worker.put("noGive", getWorkNoGived(worker));

			// 获取某个工人累计应发
			worker.put("SHOULDGIVE", coutAdd(worker.get("shouldGive"), hisShouldGive));
			// 获取某个工人累计实发
			worker.put("REALGIVE", coutAdd(worker.get("realGive"), hisRealGive));
			// 获取某个工人累计未发
			worker.put("NOGIVE", coutAdd(worker.get("noGive"), hisNoGive));

			worker.put("recStatus", "1");// 工资状态（1待审核 2未发放 3部分发放 4全部发放）

			list.add(worker);
		}

		return list;
	}

	public static String coutAdd(String A, Double B) {
		Double a = Double.parseDouble(Utils.isNull(A) ? "0" : A);
		Double total = a + B;
		DecimalFormat df = new DecimalFormat("##################0.00"); // 保留两位小数
		String restStr = df.format(total);
		return restStr;
	}

	/**
	 * 删除统计记录
	 * 
	 * @param params
	 * @return
	 */
	public static int salaryRecDel(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String workerId = params.get("workerId");
		String recTime = params.get("recTime");
		String sqladd = "";
		sqladd = "delete from t_worker_salary where worker_id='" + workerId + "' and rec_time='" + recTime + "'";
		return dao.insert(sqladd);
	}

	/**
	 * 
	 * 获取某个工人本月考勤总工时
	 * 
	 * @param params
	 * @return
	 */
	public static String getWorkHours(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String workerId = params.get("JOBNO");
		String recTime = params.get("recTime");
		String sql = "select sum(IF((SELECT MAX(REC_ID)  FROM t_attendance_record_data WHERE  REC_ID =t.REC_ID AND ATTENDANCE_STATUS='4') IS NULL,  "+
		    " CAST(t.WORKING_HOURS- IF((SELECT SUM(LATE_TIME) FROM t_attendance_record_data WHERE  REC_ID =t.REC_ID AND ATTENDANCE_STATUS='5' )IS NULL,0, "+
		    " (SELECT SUM(LATE_TIME) FROM t_attendance_record_data WHERE  REC_ID =t.REC_ID AND ATTENDANCE_STATUS='5' ))/60 AS DECIMAL(10,2)), '0.0')) WORKING_HOURS "+
		    " from t_attendance_record t "+
		    " WHERE t.JOBNO='" + workerId+ "' AND t.ATTENCE_TIME LIKE '" + recTime + "%'  ";
		List<?> l1 = dao.selectList(sql);
		Double total = 0.0;// 本月总工时小时
		if (l1.size() > 0 && l1.get(0)!=null) {
			String WORKING_HOURS = ((Map) l1.get(0)).get("WORKING_HOURS") + "";
			total = Double.parseDouble(Utils.isNull(WORKING_HOURS) ? "0" : WORKING_HOURS);
		}else{
			total=0.00;
		}
		DecimalFormat df = new DecimalFormat("######0.00"); // 保留2位小数
		String restStr = df.format(total);
		return restStr;
	}

	/**
	 * 
	 * 获取某个工人本月累计工作量
	 * 
	 * @param params
	 * @return
	 */
	public static String getWorkQuantity(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String workerId = params.get("workerId");
		String recTime = params.get("recTime");
		Double total = 0.0;
		String sql = "SELECT IF(SUM(t.PIECE_NUM) IS NULL,0,SUM(t.PIECE_NUM))  PIECE_NUM  FROM  t_piece_apply_worker t,t_process_result r "
				+ "WHERE r.SUBMIT_ID=t.apply_id AND r.REC_UPDATETIME LIKE '" + recTime + "%' AND t.worker_id='" + workerId + "'  and r.AUDIT_RESULT='1' ";
		List<?> l = dao.selectList(sql);
		if (l.size() > 0) {
			String PIECE_NUM = ((Map) l.get(0)).get("PIECE_NUM") + "";
			total = Double.parseDouble(Utils.isNull(PIECE_NUM) ? "0" : PIECE_NUM);
		}
		DecimalFormat df = new DecimalFormat("###########.######"); // 去除小数
		String restStr = df.format(total);
		return restStr;
	}

	/**
	 * 
	 * 获取某个工人本月累计奖金
	 * 
	 * @param params
	 * @return
	 */
	public static String getWorkReward(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String workerId = params.get("workerId");
		String recTime = params.get("recTime");
		Double total = 0.0;
		String sql = "SELECT IF(SUM(t.REWARD_MONEY) IS NULL,0,SUM(t.REWARD_MONEY))  REWARD_MONEY  FROM  t_reward_apply t,t_process_result r "
				+ "WHERE r.SUBMIT_ID=t.rec_id AND r.REC_UPDATETIME LIKE '" + recTime + "%' AND t.WORKER_ID='" + workerId + "' and t.REWARD_TYPE='1' ";
		List<?> l = dao.selectList(sql);
		if (l.size() > 0) {
			String REWARD_MONEY = ((Map) l.get(0)).get("REWARD_MONEY") + "";
			total = Double.parseDouble(Utils.isNull(REWARD_MONEY) ? "0" : REWARD_MONEY);
		}
		DecimalFormat df = new DecimalFormat("###########.######"); // 去除小数
		String restStr = df.format(total);
		return restStr;
	}

	/**
	 * 
	 * 获取某个工人本月累计补贴
	 * 
	 * @param params
	 * @return
	 */
	public static String getWorkSubsidy(HashMap<String, String> params) {
		Double GWSUBSIDY = Double.parseDouble(Utils.isNull(params.get("GWSUBSIDY")) ? "0" : params.get("GWSUBSIDY"));
		Double WCSUBSIDY = Double.parseDouble(Utils.isNull(params.get("WCSUBSIDY")) ? "0" : params.get("WCSUBSIDY"));
		Double QTSUBSIDY = Double.parseDouble(Utils.isNull(params.get("QTSUBSIDY")) ? "0" : params.get("QTSUBSIDY"));
		Double total = GWSUBSIDY + WCSUBSIDY + QTSUBSIDY;
		DecimalFormat df = new DecimalFormat("##################0.00"); // 保留两位小数
		String restStr = df.format(total);
		return restStr;
	}

	/**
	 * 
	 * 获取某个工人本月累计考勤扣款
	 * 
	 * @param params
	 * @return
	 */
	public static String getWorkAttDeduction(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String workerId = params.get("JOBNO");
		String recTime = params.get("recTime");
		String name= params.get("name");
		if(name.equals("陈妍希")){
			System.out.println();
		}
		Double total = 0.0;
		String sql = " SELECT SUM(IF((SELECT MAX(REC_ID)  FROM t_attendance_record_data WHERE  REC_ID =t.REC_ID AND ATTENDANCE_STATUS='4') IS NULL, "+
				" CAST(IF((SELECT SUM(AMOUNT) FROM t_attendance_record_data WHERE  REC_ID =t.REC_ID )IS NULL,0,(SELECT SUM(AMOUNT) " +
				" FROM t_attendance_record_data WHERE  REC_ID =t.REC_ID )) AS DECIMAL(10,2)),  "+
				" '0.00')) AMOUNT  "+
				" FROM t_attendance_record t  "+
				" WHERE t.JOBNO='" + workerId+ "' AND t.ATTENCE_TIME LIKE '" + recTime + "%'  ";
		List<?> l2 = dao.selectList(sql);
		if (l2.size() > 0 && l2.get(0)!=null ) {
			String AMOUNT = ((Map) l2.get(0)).get("AMOUNT") + "";
			total = Double.parseDouble(Utils.isNull(AMOUNT) ? "0" : AMOUNT);
		}else{
			total=0.00;
		}

		DecimalFormat df = new DecimalFormat("######0.00"); // 保留2位小数
		String restStr = df.format(total);
		return restStr;
	}

	/**
	 * 
	 * 获取某个工人本月累计其他扣款
	 * 
	 * @param params
	 * @return
	 */
	public static String getWorkOtherDeduction(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String workerId = params.get("workerId");
		String recTime = params.get("recTime");
		Double total = 0.0;
		String sql = "SELECT IF(SUM(t.REWARD_MONEY) IS NULL,0,SUM(t.REWARD_MONEY))  REWARD_MONEY  FROM  t_reward_apply t,t_process_result r "
				+ "WHERE r.SUBMIT_ID=t.rec_id AND r.REC_UPDATETIME LIKE '" + recTime + "%' AND t.WORKER_ID='" + workerId + "' and t.REWARD_TYPE!='1' ";
		List<?> l = dao.selectList(sql);
		if (l.size() > 0) {
			String REWARD_MONEY = ((Map) l.get(0)).get("REWARD_MONEY") + "";
			total = Double.parseDouble(Utils.isNull(REWARD_MONEY) ? "0" : REWARD_MONEY);
		}
		DecimalFormat df = new DecimalFormat("###########.######"); // 去除小数
		String restStr = df.format(total);
		return restStr;
	}

	/**
	 * 
	 * 获取某个工人本月累计请假扣款
	 * 
	 * @param params
	 * @return
	 */
	public static String getWorkAskForLeave(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String workerId = params.get("workerId");
		String recTime = params.get("recTime");
		Double total = 0.0;
		String sql = "SELECT IF(SUM(t.REWARD_MONEY) IS NULL,0,SUM(t.REWARD_MONEY))  REWARD_MONEY  FROM  t_reward_apply t,t_process_result r "
				+ "WHERE r.SUBMIT_ID=t.rec_id AND r.REC_UPDATETIME LIKE '" + recTime + "%' AND t.WORKER_ID='" + workerId + "' and t.REWARD_TYPE!='1' ";
		List<?> l = dao.selectList(sql);
		if (l.size() > 0) {
			String REWARD_MONEY = ((Map) l.get(0)).get("REWARD_MONEY") + "";
			total = Double.parseDouble(Utils.isNull(REWARD_MONEY) ? "0" : REWARD_MONEY);
		}
		DecimalFormat df = new DecimalFormat("###########.######"); // 去除小数
		String restStr = df.format(total);
		return restStr;
	}

	/**
	 * 
	 * 获取某个工人本月五险一金
	 * 
	 * @param params
	 * @return
	 */
	public static String getWorkWxyj(HashMap<String, String> params) {
		Double YLSAFE = Double.parseDouble(Utils.isNull(params.get("YLSAFE")) ? "0" : params.get("YLSAFE"));
		Double SYSAFE = Double.parseDouble(Utils.isNull(params.get("SYSAFE")) ? "0" : params.get("SYSAFE"));
		Double YWSAFE = Double.parseDouble(Utils.isNull(params.get("YWSAFE")) ? "0" : params.get("YWSAFE"));
		Double QTSAFE = Double.parseDouble(Utils.isNull(params.get("QTSAFE")) ? "0" : params.get("QTSAFE"));
		Double GJJ = Double.parseDouble(Utils.isNull(params.get("GJJ")) ? "0" : params.get("GJJ"));
		Double total = YLSAFE + SYSAFE + YWSAFE + QTSAFE + GJJ;
		DecimalFormat df = new DecimalFormat("##################0.00"); // 保留两位小数
		String restStr = df.format(total);
		return restStr;
	}

	/**
	 * 
	 * 获取某个工人本月应发
	 * 
	 * @param params
	 * @return
	 */
	public static String getWorkShouldGived(HashMap<String, String> params) {
		// 应发=基本工资+（工时、件数X单位薪酬） +奖励+补贴-考勤扣款-其他扣款-五险一金-个税-请假扣款
		// 基本工资
		Double SALARY = Double.parseDouble(Utils.isNull(params.get("SALARY")) ? "0" : params.get("SALARY"));
		// （工时、件数X单位薪酬）
		Double unitPayCount = 0.0;
		String chargeType = params.get("CHARGETYPE");
		Double UNITPAY = Double.parseDouble(Utils.isNull(params.get("UNITPAY")) ? "0" : params.get("UNITPAY"));
		Double workHours = Double.parseDouble(Utils.isNull(params.get("workHours")) ? "0" : params.get("workHours"));
		Double workQuantity = Double.parseDouble(Utils.isNull(params.get("workQuantity")) ? "0" : params.get("workQuantity"));
		if (Utils.isNotNull(chargeType)) {
			if ("1".equals(chargeType)) {// 按工程量
				unitPayCount = workQuantity * UNITPAY;
			} else if ("2".equals(chargeType)) {// 按小时
				unitPayCount = workHours * UNITPAY;
			} else if ("3".equals(chargeType)) {// 按件
				unitPayCount = workQuantity * UNITPAY;
			}
		}
		// 奖励
		Double workReward = Double.parseDouble(Utils.isNull(params.get("workReward")) ? "0" : params.get("workReward"));
		// 补贴
		Double workSubsidy = Double.parseDouble(Utils.isNull(params.get("workSubsidy")) ? "0" : params.get("workSubsidy"));
		// 考勤扣款
		Double attDeduction = Double.parseDouble(Utils.isNull(params.get("attDeduction")) ? "0" : params.get("attDeduction"));
		// 其他扣款
		Double otherDeduction = Double.parseDouble(Utils.isNull(params.get("otherDeduction")) ? "0" : params.get("otherDeduction"));
		// 五险一金
		Double wxyj = Double.parseDouble(Utils.isNull(params.get("wxyj")) ? "0" : params.get("wxyj"));
		// 个税
		Double GS = Double.parseDouble(Utils.isNull(params.get("GS")) ? "0" : params.get("GS"));
		// 请假扣款
		String askForLeave = getWorkAskForLeave(params);
		Double QJ = Double.parseDouble(Utils.isNull(askForLeave) ? "0" : askForLeave);

		Double total = SALARY + unitPayCount + workReward + workSubsidy - attDeduction - otherDeduction - wxyj - GS;
		DecimalFormat df = new DecimalFormat("##################0.00"); // 保留两位小数
		String restStr = df.format(total);
		return restStr;
	}

	/**
	 * 
	 * 获取某个工人本月实发=基本工资
	 * 
	 * @param params
	 * @return
	 */
	public static String getWorkRealGived(HashMap<String, String> params) {
		Double SALARY = Double.parseDouble(Utils.isNull(params.get("SALARY")) ? "0" : params.get("SALARY"));
		Double total = SALARY;
		DecimalFormat df = new DecimalFormat("##################0.00"); // 保留两位小数
		String restStr = df.format(total);
		return restStr;
	}

	/**
	 * 
	 * 获取某个工人本月未发
	 * 
	 * @param params
	 * @return
	 */
	public static String getWorkNoGived(HashMap<String, String> params) {
		Double SALARY = Double.parseDouble(Utils.isNull(params.get("SALARY")) ? "0" : params.get("SALARY"));
		Double shouldGive = Double.parseDouble(Utils.isNull(params.get("shouldGive")) ? "0" : params.get("shouldGive"));
		Double total = shouldGive - SALARY;
		DecimalFormat df = new DecimalFormat("##################0.00"); // 保留两位小数
		String restStr = df.format(total);
		return restStr;
	}

	/**
	 * 
	 * 获取某个工人以往的工资结果
	 * 
	 * @param params
	 * @return
	 */
	public static Map getHisSalaryByWorkId(HashMap<String, String> params) {
		PubDao dao = AppContext.getInstance().getPubDao();
		String workerId = params.get("workerId");
		String sql = " SELECT IF(SUM(SHOULD_GIVE) IS NULL,0,SUM(SHOULD_GIVE)) SHOULD_GIVE, "
				+ " IF(SUM(REAL_GIVE) IS NULL,0,SUM(REAL_GIVE)) REAL_GIVE,   "
				+ " IF(SUM(NO_GIVE) IS NULL,0,SUM(NO_GIVE)) NO_GIVE ,IF(SUM(WORK_QUANTITY) IS NULL,0,SUM(WORK_QUANTITY)) WORK_QUANTITY,IF(SUM(WORK_HOURS) IS NULL,0,SUM(WORK_HOURS)) WORK_HOURS "
				+ " FROM t_worker_salary t " + " WHERE t.REC_STATUS IN('3','4') AND t.WORKER_ID='" + workerId + "' ";
		List<?> l = dao.selectList(sql);
		return (Map) l.get(0);
	}

}
